'use strict';

const { Controller } = require('egg');
const { Op } = require('sequelize');
const serviceName = 'goods'
class RouterController extends Controller {
  // 分页
  async index() {
    const { query, service } = this.ctx;
    let { pageNum, pageSize, category_id = '', condition= '{}',  } = query;
    let page = Number(pageNum) || 1;
    let size = Number(pageSize) || 10;
    size = 9999;
    const where = {

    };
    if (category_id) {
        where.category_id = Number(category_id);
    }
    const conditionMap = JSON.parse(condition);
    if (conditionMap.server_id && conditionMap.server_id !== 'all') {
        where.server_id = Number(conditionMap.server_id);
    }
    if (conditionMap.area_id && conditionMap.area_id !== 'all') {
        where.area_id = Number(conditionMap.area_id);
    }
    // 价格区间
    if (conditionMap.price1 && conditionMap.price2) {
      where.price = {
        [Op.between]: [Number(conditionMap.price1), Number(conditionMap.price2)]
      }
    } else if (conditionMap.price3) {
      if (conditionMap.price3.includes('<')) {
        where.price = {
          [Op.lt]: Number(conditionMap.price3.replace('<', ''))
        }
      } else if (conditionMap.price3.includes('>')) {
        where.price = {
          [Op.gt]: Number(conditionMap.price3.replace('>', ''))
        }
      } else if (conditionMap.price3.includes('-')) {
        where.price = {
          [Op.between]: conditionMap.price3.split('-').map(e => Number(e))
        }
      }
    }
    if (conditionMap.content) {
      let descList = conditionMap.content.split(',');
        where.desc = {
            [Op.or]: descList.map(e => {
                return {
                    [Op.like]: `%${e}%`
                }
            })
        }
    }
    const order = [conditionMap.$sortKey, conditionMap.$sortOrder]
    const list = await service[serviceName].getPage(where, page, size, order);

    // 其他字段
    const otherField = {
      ...conditionMap,
      server_id: undefined,
        area_id: undefined,
        price1: undefined,
        price2: undefined,
        price3: undefined,
        content: undefined,
    $sortKey: undefined,
    $sortOrder: undefined,
    }
    console.log('____1______',list.rows.length)

    let _list = list.rows;
    for (const otherFieldKey in otherField) {
      if (otherField[otherFieldKey]) {
        _list = _list.filter(e => {
          // 将otherField中的每个字段作为查询条件过滤
          // 判断数组
          if (Array.isArray(condition[otherFieldKey])) {
            console.log('condition[otherFieldKey]-arr',condition[otherFieldKey])
            if (condition[otherFieldKey].length === 0) return true
            // 数组转成字符串比较
            return e.condition[otherFieldKey].join(',') === otherField[otherFieldKey].join(',')
          } else {
            console.log('condition[otherFieldKey]-srt',condition[otherFieldKey])
            if (!condition[otherFieldKey]) return true
            return e.condition[otherFieldKey] === otherField[otherFieldKey]
          }
        })
      }
    }
    console.log('_____2_____',_list.length)
    this.ctx.success({
      ...list,
      rows: _list
    });
  }
  // 新增
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    await ctx.service[serviceName].create(data);
    ctx.success(data, 200);
  }
  // 修改
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const infos = ctx.request.body;
    if (!id) throw new Error('必须传递ID参数');
    if (Object.keys(infos).length === 0) throw new Error('请传递修改的内容');
    await ctx.service[serviceName].update(id, infos);
    ctx.success(id, 200);
  }
  // 删除
  async destroy() {
    const uid = Number(this.ctx.params.id);
    if (!uid) throw new Error('ID 有误');
    await this.ctx.service[serviceName].deleteById(uid);
    this.ctx.success(uid); // 删除成功返回被删除的用户ID
  }
  // 查看
  async show() {
    const { params, service } = this.ctx;
    const findItem = await service[serviceName].findOne(params.id);
    this.ctx.success(findItem);
  }
  // 所有列表
  async allList() {
    const { service } = this.ctx;
    const list = await service[serviceName].allList();
    this.ctx.success(list);
  }
}

module.exports = RouterController;
